<?php
/**
 * $Id$
 */
class ShortLinkClass extends BinCryptClass
{
	private $_dbName = 'short_link_db';
	private $_serverID = 16;
	private $_storeTblPrefix = 'short_link_tbl_';
	private $_cachePrefix = '___short_link::';

	public function __construct()
	{

	}

	public function getShortLink($base62ID, $getInfo = false)
	{
		global $cacheObj;
		if(empty($cacheObj))
		{
			$cacheObj = Bin::singtolen('CacheClass', 'Source');
		}
		$ID = intval($this->getDecID($base62ID));
		if(empty($ID))
		{
			return false;
		}

		$cacheKey = $this->_cachePrefix.$ID;
		$info = $cacheObj->getCache($cacheKey);
		if(empty($info))
		{
			$tblSn = $this->getTblSn($ID);
			$sql = "SELECT * FROM {$this->_dbName}.{$this->_storeTblPrefix}{$tblSn} WHERE `ID`={$ID}";
			$info = db_query_sql($sql, $this->_serverID);
			if(empty($info[0]['url']))
			{
				return false;
			}
			$cacheObj->saveCache($cacheKey, $info, 7200);
		}
		if($getInfo)
		{
			return $info;
		}
		return $info[0]['url'];
	}

	public function createShortLink($srcUrl)
	{
		if(empty($srcUrl))
		{
			return false;
		}
		$registerShortLinkID = $this->registerShortLinkID();
		if($registerShortLinkID == false || $this->changeShortLink($registerShortLinkID, $srcUrl, false) == false)
		{
			return false;
		}
		return $this->getBase62ID($registerShortLinkID);
	}

	public function changeShortLink($shortLinkID, $srcUrl, $isBase62 = true)
	{
		global $cacheObj;
		if(empty($cacheObj))
		{
			$cacheObj = Bin::singtolen('CacheClass', 'Source');
		}
		$this->dealUrl($srcUrl);
		$shortLinkID = intval($isBase62 ? $this->getDecID($shortLinkID) : $shortLinkID);
		if(empty($shortLinkID))
		{
			return false;
		}
		$tblSn = $this->getTblSn($shortLinkID);
		$sql = "UPDATE {$this->_dbName}.{$this->_storeTblPrefix}{$tblSn} SET `url`='{!{$srcUrl}!}' WHERE `ID`={$shortLinkID}";
		db_query_sql($sql, $this->_serverID);
		if(db_get_affected_rows())
		{
			$cacheObj->delCache($this->_cachePrefix.$shortLinkID);
			return true;
		}
		return false;
	}

	private function dealUrl(&$url)
	{
		$url = trim($url);
		if(in_array(strtolower(substr($url, 0, 7)), array('http://', 'https:/')) == false)
		{
			$url = 'http://'.$url;
		}
		return $url;
	}

	private function getDecID($base62ID)
	{
		return BinCryptClass::base62Dec($base62ID);
	}
	
	private function getBase62ID($decID)
	{
		return BinCryptClass::decBase62($decID);
	}
	
	private function registerShortLinkID()
	{
		$maxIDCtrlObj = Bin::singtolen('MaxIDCtrlClass', 'Source', false, 'shortLinkID');
		if($maxIDCtrlObj->_lock() == false)
		{
			return false;
		}
		$registerShortLinkID = $maxIDCtrlObj->_getMaxID() + 1;
		$sql = "INSERT IGNORE {$this->_dbName}.{$this->_storeTblPrefix}".$this->getTblSn($registerShortLinkID)." (ID,  url,  `addTime`)VALUES({$registerShortLinkID}, '', ".G_CURR_TIMESTAMP.")";
		db_query_sql($sql, $this->_serverID);
		if(db_get_affected_rows())
		{
			$maxIDCtrlObj->_setMaxID($registerShortLinkID);
			$maxIDCtrlObj->_unlock();
			return $registerShortLinkID;
		}
		$maxIDCtrlObj->_unlock();
		return false;
	}

	private function getTblSn($ID)
	{
		return intval($ID % 100);
	}
}